using System;
using UnityEngine;

public class Door : MonoBehaviour
{
    public enum DoorDirection
    {
        Up,
        Down,
        Left,
        Right
    };
    public float xoffset,yoffset;
    public DoorDirection doorDirection;
    public Transform player;

    private void Start()
    {
        xoffset = 7f;
        yoffset = 12f;
        player = GameObject.FindGameObjectWithTag("Player").transform;
    }

    private void Update()
    {
        player = GameObject.FindGameObjectWithTag("Player").transform;
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.CompareTag("Player"))
        {
            MoveToNextRoom(doorDirection);
        }
    }
    public void MoveToNextRoom(Door.DoorDirection direction)
    {
        // 根据玩家当前房间的位置和方向计算下一个房间的位置
        Vector3 currentRoomPosition = player.transform.position;
        // 计算下一个房间的位置   
        Vector3 nextRoomPosition = currentRoomPosition;
        switch (direction)
        {
            case Door.DoorDirection.Up:
                nextRoomPosition += new Vector3(0, yoffset , 0);
                break;
            case Door.DoorDirection.Down:
                nextRoomPosition += new Vector3(0, -yoffset , 0);
                break;
            case Door.DoorDirection.Left:
                nextRoomPosition += new Vector3(-xoffset , 0, 0);
                break;
            case Door.DoorDirection.Right:
                nextRoomPosition += new Vector3(xoffset , 0, 0);
                break;
        }

        // 将玩家移动到下一个房间的位置
        player.transform.position = nextRoomPosition;
    }
}